﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.BasicData.ProductDetails;
using WMS.Common;
using WMS.InventoryManagement.TakeStocks.DTO;
using WMS.StorageManagement.RepositoryDefinitions;
using WMS.Web.Controllers.BasicData.Vendors;

namespace WMS.InventoryManagement.TakeStocks
{
    /// <summary>
    /// 实现接口
    /// </summary>
    public class TakeStockAppService: IApplicationService, ITakeStockAppService
    {
        /// <summary>
        /// 盘点仓储接口
        /// </summary>
        private readonly IRepository<TakeStock,int> _takeStockRepository;

        /// <summary>
        /// 商品接口
        /// </summary>
        private readonly IRepository<ProductDetail, int> _productDetailRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        public TakeStockAppService(IRepository<TakeStock, int> takeStockRepository, IRepository<ProductDetail, int> productDetailRepository)
        {
            _takeStockRepository = takeStockRepository;
            _productDetailRepository = productDetailRepository;
        }


        /// <summary>
        /// 获取供应商（分页）
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">页容量</param>
        /// <param name="chineseName">中文名字</param>
        /// <returns>返回对应数据</returns>
        public async Task<PagedResult> GetTakeStock(int page, int limit)
        {
            var list = System.Linq.PredicateBuilder.New<TakeStock>(true);
            var vendorList = await _takeStockRepository.GetListAsync(list);
            PagedResult pageResult = new PagedResult();
            pageResult.RowCount = page;
            pageResult.PageCount = limit;
            pageResult.CurrentPage = vendorList.Count();
            pageResult.Queryable = vendorList.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return pageResult;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="vendor">供应商信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddTakeStock(TakeStock takeStock)
        {

            // 等待 GetListAsync 完成并得到 ProductDetail 列表
            var productDetails = await _productDetailRepository.GetListAsync(x => x.ProductName == takeStock.Good);

            // 假设 GetListAsync 返回的是一个列表，你需要先确定你要操作的产品详情
            // 这里我假设你只需要第一个匹配的产品详情
            if (productDetails != null && productDetails.Any())
            {
                var productDetail = productDetails.First();
                takeStock.CountQuantity = productDetail.SplitNumber;

                await _takeStockRepository.InsertAsync(takeStock);
                return 1;
            }
            else
            {
                // 如果没有找到匹配的产品详情，你需要处理这种情况
                // 这里我简单的返回了 0，表示没有受影响的行数
                return 0;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id">主键Id</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteTakeStock(int Id)
        {
            await _takeStockRepository.DeleteAsync(Id);
            return 1;
        }

        /// <summary>
        /// 获取对应数据
        /// </summary>
        /// <param name="Id">主键Id</param>
        /// <returns>返回受影响行数</returns>
        public async Task<TakeStock> BackfillData(int Id)
        {
            var takeStockList = await _takeStockRepository.FindAsync(Id);
            return takeStockList;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public async Task<int> UpdateTakeStock(TakeStockDTO takeStock)
        {
            var list = takeStock.MapTo<TakeStock>();
            await _takeStockRepository.UpdateAsync(list);
            return 1;
        }

        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<ProductDetail>> GetProductDetail()
        {
            var productDetailList = await _productDetailRepository.GetListAsync();
            return productDetailList;
        }

    }
}

